<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['docdemo:student:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['docdemo:student:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['docdemo:student:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['docdemo:student:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-download" size="mini" :disabled="single" @click="handleExportDoc"
          v-hasPermi="['docdemo:student:exportDoc']">Doc档案</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-download" size="mini" :disabled="single" @click="handleExportDocx"
          v-hasPermi="['docdemo:student:exportDoc']">Docx档案</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-download" size="mini" :disabled="single" @click="handleExportPdf"
          v-hasPermi="['docdemo:student:exportDoc']">PDF档案</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="学号" align="center" prop="code" />
      <el-table-column label="学院" align="center" prop="college" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="年级" align="center" prop="gradeLevel" />
      <el-table-column label="班级" align="center" prop="classLevel" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
      <!-- <el-table-column label="照片" align="center" prop="pic" /> -->
      <el-table-column label="照片">
        　　<template slot-scope="scope">
          　　　　<img :src="scope.row.pic" width="40" height="40" class="head_pic" />
          　　</template>
      </el-table-column>
      <el-table-column label="姓名拼音" width="120" align="center" prop="nameEn" />
      <el-table-column label="宿舍号" align="center" prop="dormitory" />
      <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出生地" align="center" prop="birthPlace" />
      <el-table-column label="籍贯" align="center" prop="oriPlace" />
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="政治面貌" align="center" prop="poCategory" />
      <el-table-column label="身份证号" width="220" align="center" prop="idno" />
      <el-table-column label="联系电话" width="120" align="center" prop="mobile" />
      <el-table-column label="电子邮箱" width="120" align="center" prop="mail" />

      <el-table-column label="个人签名">
        　　<template slot-scope="scope">
          　　　　<img :src="scope.row.sgPic" width="40" height="40" class="head_pic" />
          　　</template>
      </el-table-column>
      <!-- <el-table-column label="个人签名" align="center" prop="sgPic" /> -->
      <el-table-column label="教育经历" show-overflow-tooltip width="120" align="center" prop="studyHis" />
      <el-table-column label="获奖经历" show-overflow-tooltip width="120" align="center" prop="rewardHis" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['docdemo:student:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['docdemo:student:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学生档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="code">
          <el-input v-model="form.code" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input v-model="form.college" placeholder="请输入学院" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="年级" prop="gradeLevel">
          <el-input v-model="form.gradeLevel" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="班级" prop="classLevel">
          <el-input v-model="form.classLevel" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名拼音" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入姓名拼音" />
        </el-form-item>
        <el-form-item label="宿舍号" prop="dormitory">
          <el-input v-model="form.dormitory" placeholder="请输入宿舍号" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable size="small" v-model="form.birthDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出生地" prop="birthPlace">
          <el-input v-model="form.birthPlace" placeholder="请输入出生地" />
        </el-form-item>
        <el-form-item label="籍贯" prop="oriPlace">
          <el-input v-model="form.oriPlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="poCategory">
          <el-input v-model="form.poCategory" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idno">
          <el-input v-model="form.idno" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="照片">
          <imageUpload v-model="form.pic" />
        </el-form-item>
        <el-form-item label="个人签名">
          <imageUpload v-model="form.sgPic" />
        </el-form-item>
        <el-form-item label="教育经历" prop="studyHis">
          <el-input v-model="form.studyHis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="获奖经历" prop="rewardHis">
          <el-input v-model="form.rewardHis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listStudent,
    getStudent,
    delStudent,
    addStudent,
    updateStudent,
    exportStudent,
    exportDocStudent
  } from "@/api/docdemo/student";
  import ImageUpload from '@/components/ImageUpload';

  export default {
    name: "Student",
    components: {
      ImageUpload,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学生档案表格数据
        studentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 性别字典
        sexOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          code: null,
          college: null,
          major: null,
          gradeLevel: null,
          classLevel: null,
          sex: null,
          nameEn: null,
          dormitory: null,
          birthDate: null,
          birthPlace: null,
          oriPlace: null,
          nation: null,
          poCategory: null,
          idno: null,
          mobile: null,
          mail: null,
          pic: null,
          sgPic: null,
          studyHis: null,
          rewardHis: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "姓名不能为空",
            trigger: "blur"
          }],
          nameEn: [{
            required: true,
            message: "姓名拼音不能为空",
            trigger: "blur"
          }],
          dormitory: [{
            required: true,
            message: "宿舍号不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_user_sex").then(response => {
        this.sexOptions = response.data;
      });
    },
    methods: {
      /** 查询学生档案列表 */
      getList() {
        this.loading = true;
        listStudent(this.queryParams).then(response => {
          this.studentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 性别字典翻译
      sexFormat(row, column) {
        return this.selectDictLabel(this.sexOptions, row.sex);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          code: null,
          college: null,
          major: null,
          gradeLevel: null,
          classLevel: null,
          sex: null,
          nameEn: null,
          dormitory: null,
          birthDate: null,
          birthPlace: null,
          oriPlace: null,
          nation: null,
          poCategory: null,
          idno: null,
          mobile: null,
          mail: null,
          pic: null,
          sgPic: null,
          studyHis: null,
          rewardHis: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加学生档案";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStudent(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改学生档案";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStudent(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addStudent(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除学生档案编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有学生档案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      /** 导出doc按钮操作 */
      handleExportDoc(row) {
        const ids = row.id || this.ids;
        this.loading = true;
        exportDocStudent(ids, 'doc').then(response => {
          this.download(response.msg);
          this.loading = false;
          this.msgSuccess("导出成功");
        })
      },
      /** 导出docx按钮操作 */
      handleExportDocx(row) {
        const ids = row.id || this.ids;
        this.loading = true;
        exportDocStudent(ids, 'docx').then(response => {
          this.download(response.msg);
          this.loading = false;
          this.msgSuccess("导出成功");
        })
      },
      /** 导出pdf按钮操作 */
      handleExportPdf(row) {
        const ids = row.id || this.ids;
        this.loading = true;
        exportDocStudent(ids, 'pdf').then(response => {
          this.download(response.msg);
          this.loading = false;
          this.msgSuccess("导出成功");
        })
      }
    }
  };
</script>
